package edu.gatech.app4args.utils;

import edu.gatech.app4args.resources.ResourceLoader;

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 * Created so that tree nodes would know to display the label for the
 * CompendiumNode instead of its toString() method. Also shows the icon for
 * the node instead of the default folder/file icons.
 *
 * @author Andrew Lunsford
 */
public class CustomNodeRenderer extends DefaultTreeCellRenderer {

	/**
	 * Creates a Component using the tree node data. The label and icon are set,
	 * but the rest are vanilla.
	 *
	 * @return the modified Component
	 */
	@Override
	public Component getTreeCellRendererComponent(
			JTree tree,
			Object value,
			boolean sel,
			boolean expanded,
			boolean leaf,
			int row,
			boolean hasFocus) {

		super.getTreeCellRendererComponent(
				tree, value, sel,
				expanded, leaf, row,
				hasFocus);

		// TODO fix maybe? got an error once about the casting
		// prettier way of doing this?
		CompendiumNode tempNode = ((CustomTreeNode)value).getCNode();
		
		setText(tempNode.getLabel());
		// check for supported node types, ie 1-10
		if(tempNode.getType()>0
			&& tempNode.getType()<11){
			setIcon(ResourceLoader.nodeImage(tempNode.getType()));
		}
		return this;
	}

}
